﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.AccountSer;
using Tebbee.UI.Client.UtilitiesSer;

namespace Tebbee.UI.Client.Controllers
{
    public class AdminMemberController : ApiController
    {
        private readonly AccountSerClient db = new AccountSerClient();
        private readonly UtilitiesClient utilities = new UtilitiesClient();

        public HttpResponseMessage ResetPassword(string id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.ResetPassword(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        } 

        public HttpResponseMessage AddNewMember(MemberFull mem)
        {
            if (ModelState.IsValid)
            {
                string msg = "";
                if (db.AddNewMember(out msg, mem) != 0)
                {
                    var fileName = "Email_DangKyThanhCong.html";
                    var subjecfile = "Subject_DangKyThanhCong.html";
                    var filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + fileName);
                    var subjectPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Advertise/Email/" + subjecfile);
                    try
                    {
                        var Body = utilities.ReadFile(filePath);
                        Body = Body.Replace("#tb{UserName}", mem.UserName);
                        var Subject = utilities.ReadFile(subjectPath);
                        utilities.SendMail(mem.Email,Body,Subject);
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                    }
                    return Request.CreateResponse(HttpStatusCode.Created, mem);
                }
                else
                {
                    HttpError err = new HttpError();
                    err.Message = msg;
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
                }
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        public HttpResponseMessage EditMember(MemberFull mem)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            string msg = "";
            if (db.EditProfilesMember(out msg, mem) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, mem);
            }
            else
            {
                HttpError err = new HttpError();
                err.Message = msg;
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
            }
        }

        public HttpResponseMessage DeleteMember(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.DeleteMember(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        public IEnumerable<Member_List_Result> GetMembers(int id)
        {
            var queryString = HttpUtility.ParseQueryString(Request.RequestUri.Query);
            if (id < 1) id = 1;
            id = (id - 1) * 10;
            var tmp = db.ListMembers(id, queryString["value"]);
            return tmp;
        }

        
        
    }
}
